package com.livepan.server.utils;

import com.livepan.server.service.ProblemService;
import com.livepan.server.service.model.ProblemModel;
import com.livepan.server.service.model.SubmissionModel;
import org.springframework.scheduling.annotation.Async;

import java.io.IOException;
import java.util.List;

public class AsyncUtils {
    private static AsyncUtils instance;

    public static AsyncUtils getInstance() {
        if (instance == null) {
            synchronized (AsyncUtils.class) {
                instance = new AsyncUtils();
            }
        }
        return instance;
    }

    private AsyncUtils() {

    }

    @Async
    public synchronized void rejudgeServer(ProblemService problemService, Integer problemId, List<SubmissionModel> submissionModelList) throws IOException {
        ProblemModel problemModel = problemService.getProblemByID(problemId);
        for (SubmissionModel submissionModel : submissionModelList) {
            if (problemId == null || !problemId.equals(problemModel.getProblemId())) {
                problemModel = problemService.getProblemByID(submissionModel.getProblemId());
            }
            PushJudgerUtil.getInstance().pushJudge(submissionModel, problemModel);
        }
    }
}
